package cn.adelyn.blog.blogmanager.dao;

import cn.adelyn.blog.blogmanager.dao.mapper.BlogMapper;
import cn.adelyn.blog.blogmanager.pojo.bo.InsertBlogDecryptBO;
import cn.adelyn.blog.blogmanager.pojo.dto.BlogSearchPageDTO;
import cn.adelyn.blog.blogmanager.pojo.model.BlogModel;
import cn.adelyn.blog.blogmanager.pojo.model.BlogTagModel;
import cn.adelyn.blog.blogmanager.pojo.vo.BlogVO;
import cn.adelyn.common.core.cglib.CglibUtil;
import cn.adelyn.common.core.pojo.vo.PageVO;
import cn.adelyn.common.database.pojo.dto.PageDTO;
import cn.adelyn.common.database.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Objects;

/**
 * @author chengzelee
 * @date 2023/6/3 16:52
 * @describe 封装了 blog 数据库操作，
 *           因为没有单独操作 blog-pic blog-file 的web接口,故不拆分
 *           需要独立的 blog-tag 操作接口，故拆分
 */
@Repository
@Slf4j
public class BlogDAOService {

    @Autowired
    BlogMapper blogMapper;

    @Autowired
    BlogFileDAOService blogFileDAOService;

    @Autowired
    BlogPicDAOService blogPicDAOService;

    @Autowired
    BlogTagDAOService blogTagDAOService;

    public void insertBlog(InsertBlogDecryptBO insertBlogDecryptBO) {
        BlogModel blogModel = CglibUtil.copy(insertBlogDecryptBO, BlogModel.class);
        blogMapper.insert(blogModel);

        insertBlogPicAndFile(insertBlogDecryptBO);
        insertBlogTags(insertBlogDecryptBO);
    }

    public void updateBlog(InsertBlogDecryptBO insertBlogDecryptBO) {
        BlogModel blogModel = CglibUtil.copy(insertBlogDecryptBO, BlogModel.class);
        blogMapper.updateById(blogModel);

        insertBlogPicAndFile(insertBlogDecryptBO);
        updateBlogTags(insertBlogDecryptBO);
    }

    public void deleteBlog(Long blogId) {
        QueryWrapper<BlogModel> queryWrapper = new QueryWrapper();
        queryWrapper.lambda()
                .eq(BlogModel::getBlogId, blogId);
        blogMapper.delete(queryWrapper);

        blogTagDAOService.deleteBlogTag(blogId);
        blogPicDAOService.deleteBlogPics(blogId);
        blogFileDAOService.deleteBlogFiles(blogId);
    }

    public BlogVO getBlogInfo(Long blogId) {

        BlogModel blogModel = blogMapper.selectById(blogId);

        BlogVO blogVO = CglibUtil.copy(blogModel, BlogVO.class);
        blogVO.setBlogTagList(blogTagDAOService.selectBlogTags(blogVO.getBlogId()));

        return blogVO;
    }

    public PageVO<BlogVO> getBlogPage(BlogSearchPageDTO blogSearchPageDTO, PageDTO pageDTO) {

        MPJLambdaWrapper<BlogModel> mpjLambdaQueryWrapper = JoinWrappers.lambda(BlogModel.class)
                .select(BlogModel::getBlogId, BlogModel::getBlogTitle)
                .leftJoin(BlogTagModel.class, BlogTagModel::getBlogId, BlogModel::getBlogId)
                .eq(Objects.nonNull(blogSearchPageDTO.getBlogTagId()), BlogTagModel::getTagId, blogSearchPageDTO.getBlogTagId())

                .eq(Objects.nonNull(blogSearchPageDTO.getAuthorId()), BlogModel::getAuthorId, blogSearchPageDTO.getAuthorId())
                .eq(Objects.nonNull(blogSearchPageDTO.getBlogVisible()), BlogModel::getBlogVisible, blogSearchPageDTO.getBlogVisible())
                .between(Objects.nonNull(blogSearchPageDTO.getNotAfter()) && Objects.nonNull(blogSearchPageDTO.getNotBefore()),
                        BlogModel::getCreateTime,blogSearchPageDTO.getNotBefore(), blogSearchPageDTO.getNotAfter())
                .distinct();

        Page<BlogModel> page = blogMapper.selectJoinPage(PageUtil.getPage(pageDTO), BlogModel.class, mpjLambdaQueryWrapper);

        PageVO<BlogVO> pageVO = PageUtil.getPageVO(page, BlogVO.class);

        pageVO.getList().forEach(blogVO -> {
            blogVO.setBlogTagList(blogTagDAOService.selectBlogTags(blogVO.getBlogId()));
        });

        return pageVO;
    }

    private void insertBlogPicAndFile(InsertBlogDecryptBO insertBlogDecryptBO) {
        if (insertBlogDecryptBO.getFileIdList() != null && !insertBlogDecryptBO.getFileIdList().isEmpty()) {
            blogFileDAOService.insertBlogFiles(insertBlogDecryptBO);
        }

        if (insertBlogDecryptBO.getPicIdList() != null && !insertBlogDecryptBO.getPicIdList().isEmpty()) {
            blogPicDAOService.insertBlogPics(insertBlogDecryptBO);
        }
    }

    private void insertBlogTags(InsertBlogDecryptBO insertBlogDecryptBO) {
        if (insertBlogDecryptBO.getTagIdList() != null && !insertBlogDecryptBO.getTagIdList().isEmpty()) {
            blogTagDAOService.insertBlogTag(insertBlogDecryptBO);
        }
    }

    private void updateBlogTags(InsertBlogDecryptBO insertBlogDecryptBO) {
        blogTagDAOService.deleteBlogTag(insertBlogDecryptBO.getBlogId());
        insertBlogTags(insertBlogDecryptBO);
    }
}
